﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Mimoza.GUI;

namespace Mimoza.RdpPlugin
{
    public partial class GeneralPage : Mimoza.GUI.WizardPage
    {
        private RdpData m_data;
        public GeneralPage()
        {
            InitializeComponent();
        }

        override public void Init()
        {
            m_data = this.Wizard.UserData as RdpData;

            textBoxSessionName.Text = m_data.SessionName;
            checkBoxInStartup.Checked = m_data.IsStartOnStartup;
            textBoxComputer.Text = m_data.Computer;
            textBoxUser.Text = m_data.UserName;

            this.textBoxUser.TextChanged += new System.EventHandler(this.textBoxUser_TextChanged);
            this.textBoxComputer.TextChanged += new System.EventHandler(this.textBoxComputer_TextChanged);
            this.checkBoxInStartup.CheckedChanged += new System.EventHandler(this.checkBoxInStartup_CheckedChanged);
            this.textBoxSessionName.TextChanged += new System.EventHandler(this.textBoxSessionName_TextChanged);

            this.IsConfigured = PageIsConfigured();
        }

        override public bool Try2LeavePage(WizardPageLeavingDirection direction)
        {
            if (direction == WizardPageLeavingDirection.Back)
            {
                return true;
            }

            if (!CheckSessionName()) return false;
            if (!CheckComputerName()) return false;

            //this.IsConfigured = true;
            m_data.SessionName = textBoxSessionName.Text;
            m_data.IsStartOnStartup = checkBoxInStartup.Checked;
            m_data.Computer = textBoxComputer.Text;
            m_data.UserName = textBoxUser.Text;

            return true;
        }

        bool CheckSessionName()
        {
            if (string.IsNullOrEmpty(textBoxSessionName.Text))
            {
                MessageBox.Show(Resource.EmptySessionMessage, Resource.WrongParametersCaption, 
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                textBoxSessionName.Focus();
                return false;
            }
            return true;
        }

        bool CheckComputerName()
        {
            if (string.IsNullOrEmpty(textBoxComputer.Text))
            {
                MessageBox.Show(Resource.EmptyComputerNameMessage, Resource.WrongParametersCaption,
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                textBoxComputer.Focus();
                return false;
            }
            return true;
        }

        private bool PageIsConfigured()
        {
            bool res = true;
            if (this.textBoxComputer.Text == null || this.textBoxComputer.Text.Length <= 0)
            {
                res = false;
            }
            if (this.textBoxSessionName.Text == null || this.textBoxSessionName.Text.Length <= 0)
            {
                res = false;
            }
            return res;
        }

        private void textBoxComputer_TextChanged(object sender, EventArgs e)
        {
            this.IsConfigured = PageIsConfigured();
            this.RaiseOnPageModified();
        }

        private void textBoxSessionName_TextChanged(object sender, EventArgs e)
        {
            this.IsConfigured = PageIsConfigured();
            this.RaiseOnPageModified();
        }

        private void checkBoxInStartup_CheckedChanged(object sender, EventArgs e)
        {
            this.IsConfigured = PageIsConfigured();
            this.RaiseOnPageModified();
        }

        private void textBoxUser_TextChanged(object sender, EventArgs e)
        {
            this.IsConfigured = PageIsConfigured();
            this.RaiseOnPageModified();
        }
    }
}
